<?php
/*
本系统由苏酷伊基于SiteHub项目二次开发
作者博客： https://www.sukuy.com/
后续更新地址： https://www.sukuy.com/navigation
作者QQ：3288637559
QQ交流群：884250547
任何第三方非官网在发布本系统时，不得删除该注释、版权信息、作者信息及官网等内容
*/
define('IN_ADMIN', true);
include("../includes/common.php");
$act = _get('act');
@header('Content-Type: application/json; charset=UTF-8');
switch ($act) {
    case 'form':
        
        $name         = _post('name', '');
        $url          = _post('url', '');
        $catename     = _post('catename', '');
        $keywords     = _post('keywords', '');
        $introduce    = _post('introduce', '');
        $favicon      = _post('favicon', '');
        $icp          = _post('icp', '');
        $link_location = _post('link_location', '');
        $contact      = _post('contact', '');
        if (!$url) {
            exit('{"code":-1,"msg":"网站网址不能为空！"}');
        }
        $site_has = $DB->count('site', array('url' => $url));
        if ($site_has) {
            exit('{"code":-1,"msg":"该站点已经存在，请勿重复提交！"}');
        }
        $apply_has = $DB->count('apply', array('url' => $url));
        if ($apply_has) {
            exit('{"code":-1,"msg":"该站点已提交过，请勿重复提交！"}');
        }
        if (!$name) {
            exit('{"code":-1,"msg":"网站名称不能为空！"}');
        }
        if (!$catename) {
            exit('{"code":-1,"msg":"网站分类不能为空！"}');
        }
        if (!$introduce) {
            exit('{"code":-1,"msg":"网站简介不能为空！"}');
        }
        $site_max_lid = $DB->find('site', 'MAX(lid) as max_lid', null);
        $apply_max_lid = $DB->find('apply', 'MAX(lid) as max_lid', null);
        $site_lid = ($site_max_lid && $site_max_lid['max_lid']) ? $site_max_lid['max_lid'] : 0;
        $apply_lid = ($apply_max_lid && $apply_max_lid['max_lid']) ? $apply_max_lid['max_lid'] : 0;
        $new_lid = max($site_lid, $apply_lid) + 1;
        $insert_data = array(
            'name'         => $name,
            'img'          => $favicon,
            'catename'     => $catename,
            'url'          => $url,
            'keywords'     => $keywords,
            'introduce'    => $introduce,
            'lid'          => $new_lid, 
        );
        if (!empty($favicon)) {
            $insert_data['favicon'] = $favicon;
        }
        if (!empty($link_location)) {
            $insert_data['link_location'] = $link_location;
        }
        if (!empty($contact)) {
            $insert_data['contact'] = $contact;
        }
        if (!empty($icp)) {
            $insert_data['icp'] = $icp;
        }
        $result = $DB->insert('apply', $insert_data);
        if ($result) {
            exit('{"code":0,"msg":"提交成功，请耐心等待审核！"}');
        } else {
            $error = $DB->error();
            exit('{"code":-1,"msg":"提交失败，请重试！","error":"'.addslashes($error).'"}');
        }
        break;
    default:
        exit('{"code":-4,"msg":"No Act"}');
        break;
}